package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.service.AdminCommentService;
import com.cskaoyan.market.util.MybatisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminCommentServiceImpl implements AdminCommentService {
    @Override
    public List<MarketComment> list(String page, String limit, String sort, String order, String userId, String valueId) {
        SqlSession session = MybatisUtil.getSession();
        MarketCommentMapper mapper = session.getMapper(MarketCommentMapper.class);
        MarketCommentExample marketCommentExample = new MarketCommentExample();

        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (!StringUtils.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        marketCommentExample.setOrderByClause(sort + " " + order);
        List<MarketComment> marketComments = mapper.selectByExample(marketCommentExample);
        session.commit();
        session.close();
        return marketComments;
    }

    @Override
    public Integer delete(String id) {
        SqlSession session = MybatisUtil.getSession();
        MarketCommentMapper mapper = session.getMapper(MarketCommentMapper.class);
        MarketCommentExample marketGoodsExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIdEqualTo(Integer.parseInt(id));
        int i = mapper.deleteByExample(marketGoodsExample);
        session.commit();
        session.close();
        return i;
    }
}
